/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.raflik.queryxml.ui.model;

import java.util.ArrayList;
import java.util.List;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author raflik
 */
public class ResultTreeModel implements TreeModel {

    private final List<Object> listModel = new ArrayList<Object>();

    public ResultTreeModel(Object obj) {
        if (obj == null) {
            return;
        }
        if (obj instanceof NodeList) {
            NodeList nodeList = (NodeList) obj;
            int length = nodeList.getLength();
            for (int i = 0; i < length; i++) {
                Node node = nodeList.item(i);
                listModel.add(node);
            }
        } else if (obj instanceof List) {
            List<?> list = (List) obj;
            listModel.addAll(list);
        } else {
            listModel.add(obj);
        }
    }

    public Object getRoot() {
        return listModel;
    }

    public Object getChild(Object parent, int index) {
        return listModel.get(index);
    }

    public int getChildCount(Object parent) {
        return listModel.size();
    }

    public boolean isLeaf(Object node) {
        return node == getRoot() ? false : true;
    }

    public void valueForPathChanged(TreePath path, Object newValue) {
        //nothing
    }

    public int getIndexOfChild(Object parent, Object child) {
        return listModel.indexOf(child);
    }

    public void addTreeModelListener(TreeModelListener l) {
        //nothing
    }

    public void removeTreeModelListener(TreeModelListener l) {
        //nothing
    }
}
